<?php
declare (strict_types = 1);

namespace app\admin\controller;

use think\Exception;
use think\exception\ValidateException;

class Image extends Base {

    protected $notCheckTokenAction = ["upload", "layUpload"];

    public function upload() {
        $file = $this->request->file("file");

        $filePath = "";
        try{
            $filePath = (new \app\business\Image())->save($file, "product");
        }catch (ValidateException $e){
            return show(config("status.error"), $e->getMessage());
        }catch(Exception $e){
            return show(config("status.error"), $e->getMessage());
        }

        return show(config("status.success"), "ok", ["image" => $filePath]);
    }

    public function layUpload() {
        $file = $this->request->file("file");

        $filePath = "";
        try{
            $filePath = (new \app\business\Image())->save($file, "product");
        }catch (ValidateException $e){
            return layShow(config("status.layEdit.error"), $e->getMessage());
        }catch(Exception $e){
            return layShow(config("status.layEdit.error"), $e->getMessage());
        }

        return layShow(config("status.layEdit.success"), "ok", $filePath);
    }

}